﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

/// <summary>
/// Descripción breve de SelectBuilderData
/// </summary>
/// 
public class FilterDataBinder
{
    private List<WhereFilter> filters;
    public List<WhereFilter> Filters
    {
        get { return filters; }
      
    }
    private enum SupportedWebControls { TextBox, DropDownList, DropDowListArray };
    public FilterDataBinder()
    {
        filters = new List<WhereFilter>();
    }
    public void AddFilterData(string columnName,object controlSource,WhereFilter.WhereOperation operacion,WhereFilter.SQLValueType tipo )
    {
        filters.Add(new WhereFilter(columnName, controlSource, null, operacion, tipo));
    }
    private const string NullTextValue = "- -";
    public void BindData()
    {
        

        foreach (WhereFilter filter in filters)
        {
            switch (GetEnumType(filter.SourceData))
            {
                case SupportedWebControls.DropDownList:
                    DropDownList ddl = (DropDownList)filter.SourceData;
                    if (ddl.Text != NullTextValue) filter.ColumnValue = ddl.Text;
                   
                    break;
                case SupportedWebControls.DropDowListArray://Se asume fecha
                    DropDownList[] ddllist = (DropDownList[])filter.SourceData;
                    DateTime? fecha = GetDateFromDownDropLists(ddllist[0], ddllist[1], ddllist[2]);
                    if (fecha.HasValue)
                        filter.ColumnValue = String.Format("{0}{1}{2}", fecha.Value.Year.ToString("0000"), fecha.Value.Month.ToString("00"), fecha.Value.Day.ToString("00"));
                   
                    break;
                case SupportedWebControls.TextBox:
                    string text = ((TextBox)filter.SourceData).Text;
                    if (text.Trim().Length > 0) filter.ColumnValue = ((TextBox)(filter.SourceData)).Text;
                    
                    break;
            }
        }

        Predicate < WhereFilter > elminarFiltrosConValorEnNull= delegate(WhereFilter item)
        {
            return item.ColumnValue == null;
        };
        filters.RemoveAll(elminarFiltrosConValorEnNull);

    }
    private SupportedWebControls GetEnumType(object value)
    {
        if (value is DropDownList) return SupportedWebControls.DropDownList;
        if (value is TextBox) return SupportedWebControls.TextBox;
        if (value is DropDownList[]) return SupportedWebControls.DropDowListArray;

        throw new ApplicationException("WebControl no soportado por FilterDataBinder");
    }

    public static DateTime? GetDateFromDownDropLists(DropDownList day, DropDownList month, DropDownList year)
    {
        //string stringdate = day.Text + '/' + month.Text + '/' + year.Text;
        //Por problemas del idioma en la que se instalo el sistema operativo se debe usar el formato ISO AAAAMMDD
        
        string stringdate = String.Format("{0}/{1}/{2}", year.Text, month.Text, day.Text);


        DateTime dateTime = new DateTime();
        if (DateTime.TryParse(stringdate, out dateTime))
        {
            return dateTime;
        }
        else
            if (stringdate == (NullTextValue + '/' + NullTextValue + '/' + NullTextValue)) return null;
            else throw new DateException();
    }

}
